//
//  UIView+HUD.m
//  RaiyiCommon
//
//  Created by zhiwei ma on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIView+HUD.h"
#import "MBProgressHUD.h"

#define RYCMBHUDViewTag 9998
#define RYCMBHUDViewForCustomViewTag 9999

@implementation UIView (HUD)
- (void)showHUD
{
    MBProgressHUD* hud = (MBProgressHUD*)[self viewWithTag:RYCMBHUDViewTag];
    if (hud)
    {
        [self bringSubviewToFront:hud];
        return;
    }
    
    hud =[MBProgressHUD showHUDAddedTo:self animated:YES];
    hud.tag = RYCMBHUDViewTag;
}

- (void)showHUD:(NSString*)aText
{
    MBProgressHUD* hud = (MBProgressHUD*)[self viewWithTag:RYCMBHUDViewTag];
    if (hud)
    {
        [self bringSubviewToFront:hud];
        return;
    }
    
    hud =[MBProgressHUD showHUDAddedTo:self animated:YES];
    hud.labelText = aText;
    hud.tag = RYCMBHUDViewTag;
}

- (void)showCustomHUD:(UIView *)customView
{
    MBProgressHUD* hud = (MBProgressHUD*)[self viewWithTag:RYCMBHUDViewForCustomViewTag];
    if (hud) {
        [self bringSubviewToFront:hud];
        return;
    }
    hud = [[MBProgressHUD alloc] initWithView:self] ;
    [self addSubview:hud];
    hud.customView = customView;
    hud.mode = MBProgressHUDModeCustomView;
    [hud show:YES];
    hud.tag = RYCMBHUDViewForCustomViewTag;
}

- (void)showAutoDispearCustomHUD:(UIView *)customView
{
    [self showCustomHUD:customView];
    [self performSelector:@selector(hideHUD) withObject:self afterDelay:1];
}

- (void)hideHUD
{
    [MBProgressHUD hideHUDForView:self animated:YES];
}

- (BOOL)hasHUD
{
    MBProgressHUD* hud = (MBProgressHUD*)[self viewWithTag:RYCMBHUDViewTag];
    return hud ? YES : NO;
}

@end
